package com.shijunhao.graduation.service.business.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shijunhao.graduation.domain.bean.HotFiveInfo;
import com.shijunhao.graduation.domain.entity.BaseEntity;
import com.shijunhao.graduation.domain.entity.BusinessHotInfo;
import com.shijunhao.graduation.domain.entity.SystemDictItem;
import com.shijunhao.graduation.domain.vo.UserAllCulVo;
import com.shijunhao.graduation.domain.vo.UserCulVo;
import com.shijunhao.graduation.enums.DictCode;
import com.shijunhao.graduation.enums.TableName;
import com.shijunhao.graduation.mapper.BusinessHotInfoMapper;
import com.shijunhao.graduation.service.business.*;
import com.shijunhao.graduation.service.system.ISystemDictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: shijunhao
 * @date: 2022/5/29 19:57
 * @description:
 */
@Service
public class BusinessCulServiceImpl implements IBusinessCulService {

    @Resource
    private IBusinessHotInfoService hotInfoService;
    @Resource
    private BusinessHotInfoMapper businessHotInfoMapper;
    @Resource
    private IBusinessUserInfoService businessUserInfoService;
    @Resource
    private ISystemDictService systemDictService;

    @Override
    public UserAllCulVo culSelf() {
        Long id = businessUserInfoService.getCurrentUser().getId();
        UserAllCulVo result = new UserAllCulVo();
        SystemDictItem note = systemDictService.loadByCodeLabel(DictCode.TABLE.getCode(), TableName.BUSINESS_NOTE.getLabel());
        SystemDictItem forum = systemDictService.loadByCodeLabel(DictCode.TABLE.getCode(), TableName.BUSINESS_FORUM.getLabel());
        //总浏览量
        int looks = hotInfoService.selectByCondition(Wrappers.lambdaQuery(BusinessHotInfo.class)
                .eq(BaseEntity::getCreateBy, id))
                .stream()
                .mapToInt(BusinessHotInfo::getLooks)
                .sum();
        result.setLooks(looks);
        //用户统计信息
        result.setUserCulVo(hotInfoService.toUserCulVo(id));
        //统计浏览量前五
        List<HotFiveInfo> noteFive = businessHotInfoMapper.hotNoteFive(note.getDictValue(), 1, id);
        List<HotFiveInfo> postFive = businessHotInfoMapper.hotNoteFive(note.getDictValue(), 0, id);
        List<HotFiveInfo> forumFive = businessHotInfoMapper.hotForumFive(forum.getDictValue(), id);
        result.setNoteFive(noteFive);
        result.setPostFive(postFive);
        result.setForumFive(forumFive);
        //统计论坛讨论数前五
        List<HotFiveInfo> disFive = businessHotInfoMapper.disForumFive(id);
        result.setForumDisFive(disFive);
        return result;
    }

    @Override
    public UserAllCulVo culAll() {
        UserAllCulVo result = new UserAllCulVo();
        SystemDictItem note = systemDictService.loadByCodeLabel(DictCode.TABLE.getCode(), TableName.BUSINESS_NOTE.getLabel());
        SystemDictItem forum = systemDictService.loadByCodeLabel(DictCode.TABLE.getCode(), TableName.BUSINESS_FORUM.getLabel());
        //总浏览量
        int looks = hotInfoService.selectByCondition(Wrappers.lambdaQuery(BusinessHotInfo.class))
                .stream()
                .mapToInt(BusinessHotInfo::getLooks)
                .sum();
        result.setLooks(looks);
        //设置文章相关信息
        Long id = businessUserInfoService.getCurrentUser().getId();
        result.setUserCulVo(hotInfoService.toAdminCulVo(id));
        //统计浏览量前五
        List<HotFiveInfo> noteFive = businessHotInfoMapper.hotNoteFive(note.getDictValue(), 1, null);
        List<HotFiveInfo> postFive = businessHotInfoMapper.hotNoteFive(note.getDictValue(), 0, null);
        List<HotFiveInfo> forumFive = businessHotInfoMapper.hotForumFive(forum.getDictValue(), null);
        result.setNoteFive(noteFive);
        result.setPostFive(postFive);
        result.setForumFive(forumFive);
        //统计论坛讨论数前五
        List<HotFiveInfo> disFive = businessHotInfoMapper.disForumFive(null);
        result.setForumDisFive(disFive);
        return result;
    }

}
